package com.boc.news.server;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest hreq = (HttpServletRequest) request;
		HttpServletResponse hresp = (HttpServletResponse) response;
		hreq.setCharacterEncoding("UTF-8");
		hresp.setCharacterEncoding("UTF-8");
		String reqURL = hreq.getRequestURL().toString();
		if(reqURL.indexOf("/filter/image/imgload") > -1 || reqURL.indexOf("*.AJAX") > -1) {
			arg2.doFilter(request, response);
		}else {
			Object success = hreq.getSession().getAttribute("loginseccess");
			if(success != null && success.equals("1")) {
				arg2.doFilter(request, response);
			}else{
				String webUrl = hreq.getScheme()+"://"+hreq.getServerName()+":"+hreq.getServerPort()+ hreq.getContextPath() +"/";;
				hresp.sendRedirect(webUrl+"index.html");
			}
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
			
	}

}
